package controller

import (
	"exam_go/service"
	"fmt"
	"net/http"
	"time"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

type UserController struct {
}

func (u UserController) HandleSignup(c *gin.Context) { //用户注册
	userService := service.UserService{}
	resp, err := userService.HandleSignupservice(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	timestamp := time.Now().Unix()
	fmt.Println("Unix时间戳（秒）:", timestamp)
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    resp,
	})
}

func (u UserController) HandleLogin(c *gin.Context) { // 用户登录
	userService := service.UserService{}
	resp, err := userService.HandleLoginService(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	session := sessions.Default(c)
	fmt.Printf("controller %v", session.Get("userId"))
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    resp,
	})
}

func (u UserController) HandleLogout(c *gin.Context) { //用户登出
	session := sessions.Default(c)
	var resp struct {
		Login bool `json:"login"`
	}
	if session.Get("userId") == nil {
		println(session.Get("userId"))
		resp.Login = false
		c.JSON(http.StatusOK, gin.H{
			"success": true,
			"data":    resp,
		})
		return
	}
	session.Clear()
	if err := session.Save(); err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	resp.Login = true
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    resp,
	})
}

func (u UserController) HandleGetGoodLists(c *gin.Context) { // 按类查询商品
	userService := service.UserService{}
	resp, err := userService.HandleGetGoodListsService(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    resp,
	})
}

func (u UserController) HandleOrderGood(c *gin.Context) {
	userService := service.UserService{}
	resp, err := userService.HandleOrderGoodService(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    resp,
	})
}

func (u UserController) HandleAddCart(c *gin.Context) {
	userService := service.UserService{}
	resp, err := userService.HandleAddCartService(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    resp,
	})
}

func (u UserController) HandleGetGoodInfo(c *gin.Context) {
	userService := service.UserService{}
	resp, err := userService.HandleGetGoodInfoService(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    resp,
	})
}

func (u UserController) HandleBuy(c *gin.Context) {
	userService := service.UserService{}
	resp, err := userService.HandleBuyService(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    resp,
	})
}

func (u UserController) HandleComment(c *gin.Context) { //用户评论
	userService := service.UserService{}
	resp, err := userService.HandleCommentService(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    resp,
	})
}

func (u UserController) HandleDeleteOrder(c *gin.Context) { //用户评论
	userService := service.UserService{}
	err := userService.HandleDeleteOrderService(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
	})
}

func (u UserController) HandleGetOrderInfo(c *gin.Context) { //用户查询个人订单
	userService := service.UserService{}
	resp, err := userService.HandleGetOrderInfoService(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    resp,
	})
}

func (u UserController) HandleGetCartInfo(c *gin.Context) { //用户查询个人订单
	userService := service.UserService{}
	resp, err := userService.HandleGetCartInfoService(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    resp,
	})
}

func (u UserController) HandleGetUserInfo(c *gin.Context) { //用户查询个人订单
	userService := service.UserService{}
	resp, err := userService.HandleGetUserInfoService(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    resp,
	})
}

func (u UserController) HandleGetServiceTimes(c *gin.Context) { //用户查询个人订单
	userService := service.UserService{}
	resp, err := userService.HandleGetServiceTimesService(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    resp,
	})
}

func (u UserController) HandleOrderService(c *gin.Context) {
	userService := service.UserService{}
	resp, err := userService.HandleOrderServiceService(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    resp,
	})
}

func (u UserController) HandleEditSelfInfo(c *gin.Context) {
	userService := service.UserService{}
	resp, err := userService.HandleEditSelfInfoService(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    resp,
	})
}

func (u UserController) HandleDeleteCart(c *gin.Context) {
	userService := service.UserService{}
	resp, err := userService.HandleDeleteCartService(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    resp,
	})
}

func (u UserController) HandleRefund(c *gin.Context) {
	userService := service.UserService{}
	err := userService.HandleRefundService(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
	})
}

func (u UserController) HandleUserComment(c *gin.Context) {
	userService := service.UserService{}
	err := userService.HandleUserCommentService(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
	})
}

func (u UserController) HandleEditPasswd(c *gin.Context) {
	userService := service.UserService{}
	err := userService.HandleEditPasswdService(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
	})
}

func (u UserController) HandleGetRest(c *gin.Context) {
	userService := service.UserService{}
	resp, err := userService.HandleGetRestService(c)
	if err != nil {
		fmt.Printf("controller %v", err)
		c.Error(&gin.Error{
			Err:  err,
			Type: service.SysErr,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"success": true,
		"data":    resp,
	})
}
